package com.jsmy.softapp.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jsmy.softapp.R;
import com.jsmy.softapp.adapter.DataAdapter;
import com.jsmy.softapp.aroute.AR;
import com.jsmy.softapp.bean.AppZLGroupBean;
import com.jsmy.softapp.bean.AppZLListBean;
import com.jsmy.softapp.fragment.HomeFragment;
import com.jsmy.softapp.net.API;
import com.jsmy.softapp.presenter.DataPresenter;
import com.jsmy.softapp.util.ShareUtil;
import com.jsmy.softapp.util.ToastUtil;
import com.jsmy.softapp.view.DataView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created with Android Studio.
 * User: wei_cd.
 * Date: 2018/4/23.
 * Time: 14:10.
 * To change for file | settings | file and code templates.
 */

@Route(path = AR.A_DATA)
public class DataActivity extends BaseActivity<DataView, DataPresenter> implements DataView, UMShareListener {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_fenzu)
    TextView tvFenzu;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.m_rcycler)
    RecyclerView mRcycler;
    DataAdapter adapter;
    List<AppZLListBean.DataBean.ListBean> list;
    int pageindex = 1;
    String groupcd = "";
    List<AppZLGroupBean.DataBean.ListBean> group;
    String[] items;

    String shareId;
    String sharePt;

    String fzmc = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_data;
    }

    @Override
    protected DataPresenter createPresenter() {
        return new DataPresenter();
    }

    @Override
    protected void initView() {
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DataActivity.this.finish();
            }
        });
        tvFenzu.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        adapter = new DataAdapter();
        mRcycler.setLayoutManager(new LinearLayoutManager(this));
        mRcycler.setAdapter(adapter);

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageindex = 1;
                presenter.getappsyzlgdlist(groupcd, pageindex);

            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pageindex++;
                presenter.getappsyzlgdlist(groupcd, pageindex);
            }
        });

        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.img_tip:
                        shareId = list.get(position).getId();
                        ShareUtil.shareWeb(DataActivity.this,((AppZLListBean.DataBean.ListBean)adapter.getData().get(position)).getUrl(),((AppZLListBean.DataBean.ListBean)adapter.getData().get(position)).getTitle(),((AppZLListBean.DataBean.ListBean)adapter.getData().get(position)).getNote(),DataActivity.this);
                        break;
                    case R.id.linear_bar:
                        ARouter.getInstance().build(AR.A_SHARE)
                                .withString("id", ((AppZLListBean.DataBean.ListBean)adapter.getData().get(position)).getId())
                                .withString("url", ((AppZLListBean.DataBean.ListBean)adapter.getData().get(position)).getUrl())
                                .withString("title", ((AppZLListBean.DataBean.ListBean)adapter.getData().get(position)).getTitle())
                                .withString("note", ((AppZLListBean.DataBean.ListBean)adapter.getData().get(position)).getNote())
                                .withString("share", "Y")
                                .navigation();
                        break;
                }

            }
        });
    }

    @Override
    protected void initData() {
        presenter.getappzlgrouplist();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadmore();
    }

    @Override
    public void showLoadingData(String type, Object object) {
        switch (type) {
            case API.APP_ZL_GD_LIST:
                if (!fzmc.isEmpty()){
                    tvFenzu.setText(fzmc);
                }
                list = ((AppZLListBean) object).getData().getList();
                if (pageindex == 1) {
                    refreshLayout.finishRefresh();
                    adapter.replaceData(list);
                } else {
                    refreshLayout.finishLoadmore();
                    adapter.addData(list);
                }
                break;
            case API.APP_ZL_GROUP_LIST:
                group = ((AppZLGroupBean) object).getData().getList();
                if (group.size() > 0) {
                    groupcd = group.get(0).getGroupcd();
                    presenter.getappsyzlgdlist(groupcd, pageindex);
                    items = new String[group.size()];
                    for (int i = 0; i < group.size(); i++) {
                        items[i] = group.get(i).getGroupmc();
                    }
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        presenter.model.disposable.clear();
        super.onDestroy();
    }

    @OnClick(R.id.tv_fenzu)
    public void onViewClicked() {
        showListDialog();
    }

    private void showListDialog() {
        AlertDialog.Builder listDialog =
                new AlertDialog.Builder(DataActivity.this);
        listDialog.setTitle("选择分组");
        listDialog.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // which 下标从0开始
                groupcd = group.get(which).getGroupcd();
                fzmc = group.get(which).getGroupmc();
                presenter.getappsyzlgdlist(groupcd, pageindex);
            }
        });
        listDialog.show();
    }

    @Override
    public void onStart(SHARE_MEDIA share_media) {

    }

    @Override
    public void onResult(SHARE_MEDIA platform) {
        switch (platform) {
            case QQ:
                sharePt = "QQ";
                break;
            case QZONE:
                sharePt = "QZONE";
                break;
            case SINA:
                sharePt = "SINA";
                break;
            case WEIXIN:
                sharePt = "WEIXIN";
                break;
            case WEIXIN_CIRCLE:
                sharePt = "WEIXIN_CIRCLE";
                break;
            case SMS:
                sharePt = "SMS";
                break;
            case EMAIL:
                sharePt = "EMAIL";
                break;
        }
        presenter.savefxinfo(shareId,"",sharePt,"");
    }

    @Override
    public void onError(SHARE_MEDIA share_media, Throwable throwable) {
        ToastUtil.showShort(this, "分享失败，"+throwable.getMessage());
    }

    @Override
    public void onCancel(SHARE_MEDIA share_media) {
        ToastUtil.showShort(this, "分享取消");
    }

}
